﻿Module camera
    Private vindo As WIA.Device
    Private ready As Boolean = False
    Sub Init()
        'Cycle through each WIA device
        For Each cam As WIA.DeviceInfo In New WIA.DeviceManager().DeviceInfos
            'Check if it's a video device ( and not a scanner )
            If cam.Type = WIA.WiaDeviceType.VideoDeviceType Then
                Try
                    'Try to connect, and then break out
                    'If there was an error connecting, the try block will eat it
                    'And try the next device
                    vindo = cam.Connect
                    Exit For
                Catch ex As Exception

                End Try
            End If
        Next

        'If the WIA device isnot nothing ( valid ), update ready to show it
        ready = vindo IsNot Nothing
    End Sub
    Function IsReady() As Boolean
        Return ready
    End Function
    Function TakePicture() As Image
        'This is really, really slow and I wish I could fix that.
        'But I think the nature of it prevents me from fixing it, or WIAAUT is just
        'a special child

        'In a nutshell, execute the take picture command on the device, transer if to
        'a WIA.ImageFile, casting the format to JPEG, and then return an image created
        'from a memory stream that's created from the item's byte( ) on FileData.BinaryData

        'I'm fairly certain that the hangup is there, but there isn't really anything I can do
        'about it that I am aware of
        Dim item As WIA.Item = vindo.ExecuteCommand(WIA.CommandID.wiaCommandTakePicture)
        Dim pic As WIA.ImageFile

        pic = item.Transfer(WIA.FormatID.wiaFormatJPEG)

        Return Image.FromStream(New IO.MemoryStream(CType(pic.FileData.BinaryData, Byte())))
    End Function
End Module
